<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="root"></div>
    <script>
        let obj = {
            name: 'div', // 指定渲染的标签,
            props: { // 属性对象
                class: 'box', // 类名
                id: 'boxId', // id
                key: '1',
                children: [{
                    name: 'h1',
                    props: {
                        class: 'title',
                        children: ['我是标题']
                    }
                }, {
                    name: 'ul',
                    props: {
                        class: 'list',
                        children: [{
                            name: 'li',
                            props: {
                                class: 'li',
                                children: ['列表一']
                            }
                        }, {
                            name: 'li',
                            props: {
                                class: 'li',
                                children: ['列表二']
                            }
                        }, ]
                    }
                }, ]
            }
        }

        function renderDom(obj, root) {
            if (typeof obj === "string") {
                root.innerHTML += obj
            } else {
                let dom = document.createElement(obj.name)
                if (Object.keys(obj.props).length) {
                    for (let key in obj.props) {
                        if (key === "children") {
                            obj.props[key].forEach((item) => {
                                renderDom(item, dom)
                            })

                        } else {
                            dom.setAttribute(key, obj.props[key])

                        }
                    }
                }
                root.appendChild(dom)
            }

        }
        renderDom(obj, root)
    </script>

</body>

</html>